//
//  UserViewController.m
//  XMDLearn
//
//  Created by hao on 16/4/8.
//  Copyright © 2016年 hao. All rights reserved.
//
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height//获取屏幕高度，兼容性测试
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width//获取屏幕宽度，兼容性测试


#import "AppDelegate.h"
#import "UserViewController.h"
#import "recordTableViewCell.h"
#import "VIPMemberTabCell.h"
#import "XFSegementView.h"


@interface UserViewController ()<UITableViewDelegate,UITableViewDataSource,VIPMemberCellDelegate,TouchXFSegDelegate>

@property (retain,nonatomic) UIView *headView;
//头像
@property (retain, nonatomic)  UIImageView *headImg;
//用户名
@property (retain, nonatomic)  UILabel *nameLab;
//账户类型
@property (retain, nonatomic)  UILabel *accountTypeLab;
//会员中心
@property (retain, nonatomic)  UIImageView *memberImg;
//用户操作
@property (retain,nonnull)  XFSegementView *operateXFSeg;

//信息列表
@property (strong, nonatomic)  UITableView *informationTab;

//导航栏右按钮
@property UIButton *rightBarBtn;


//信息列表数组
@property NSMutableArray *informationArray;
@property NSMutableArray *browseArray;
@property NSMutableArray *collectArray;

@property BOOL isSelectLastSeg;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _informationArray = [[NSMutableArray alloc]init];
    
    _isSelectLastSeg = NO;
    
    _browseArray = [[NSMutableArray alloc]init];
    _collectArray = [[NSMutableArray alloc]init];
    
    [self initView];
    [self initData];
    
    [_informationTab reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//初始化数据
-(void)initData{
    //
    [_browseArray addObject:@"1"];
    [_browseArray addObject:@"2"];
    [_browseArray addObject:@"3"];
    
    [_collectArray addObject:@"a"];
    [_collectArray addObject:@"b"];
    [_collectArray addObject:@"c"];
    
    _informationArray = _browseArray;
    
    
}
//初始化视图
-(void)initView{
    
    //导航栏右侧按钮
    _rightBarBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width *0.05, self.view.frame.size.width *0.05)];
    [_rightBarBtn setBackgroundImage:[UIImage imageNamed:@"setting"] forState:normal];
    [_rightBarBtn addTarget:self action:@selector(Action_setting) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc]initWithCustomView:_rightBarBtn];
    
    self.navigationItem.rightBarButtonItem = rightBarBtnItem;
    
    //
    _headView =[[UIView alloc]initWithFrame:CGRectMake2(8, 66, 304, 90)];
    [_headView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_headView];
    //头像
    _headImg = [[UIImageView alloc]initWithFrame:CGRectMake2(8, 10, 66, 66)];
    _headImg.image  = [UIImage imageNamed:@"qq"];
    [_headView addSubview:_headImg];
    //用户名称
    _nameLab = [[UILabel alloc]initWithFrame:CGRectMake2(82, 10, 214, 30)];
    [_nameLab setText:@"用户名"];
    [_headView addSubview:_nameLab];
    //账户类型
    _accountTypeLab = [[UILabel alloc]initWithFrame:CGRectMake2(82, 49, 181, 30)];
    [_accountTypeLab setText:@"超级VIP"];
    [_headView addSubview:_accountTypeLab];
    //会员中心
    _memberImg =[[UIImageView alloc]initWithFrame:CGRectMake2(266, 49, 30, 30)];
    [_memberImg setImage:[UIImage imageNamed:@"qq"]];
    [_headView addSubview:_memberImg];
    
    //用户操作
    
    _operateXFSeg = [[XFSegementView alloc]initWithFrame:CGRectMake2(8, 160, 304, 28)];
    _operateXFSeg.touchDelegate =self;
    _operateXFSeg.backgroundColor = [UIColor whiteColor];
    _operateXFSeg.scrollLineColor = [UIColor redColor];
    _operateXFSeg.tintColor = [UIColor redColor];
    _operateXFSeg.titleArray =@[@"浏览记录",@"收藏信息",@"VIP会员"];
    _operateXFSeg.haveRightLine =YES;
    [_operateXFSeg selectLabelWithIndex:0];
    [self.view addSubview:_operateXFSeg];
    
    
    
    
    //信息列表
    _informationTab = [[UITableView alloc]initWithFrame:CGRectMake2(8, 189, 304, 330)];
    _informationTab.delegate =self;
    _informationTab.dataSource = self;

 
    [self.view addSubview:_informationTab];
    

}

/**
 列表
 */
//行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ( _isSelectLastSeg == YES) {
        
        
        return _informationTab.frame.size.height;
    }
    
    return _informationTab.frame.size.height *0.15;

}

//行的数量
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_isSelectLastSeg == YES) {
        
        return 1;
    }
    
    return _informationArray.count;
    
    
    
}
//行的数据信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_isSelectLastSeg == YES) {
        
        static NSString *cellID = @"VIPMemberCellID";
        
        UINib *cellNib = [UINib nibWithNibName:@"VIPMember" bundle:nil];
        
        [tableView registerNib:cellNib forCellReuseIdentifier:cellID];
        
        
        
        VIPMemberTabCell  *tableCell= [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        
  
        
    
        
        
        tableCell.delegate = self;

        
        NSLog(@"2:%f",_informationTab.frame.size.width);
        
        tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
         return tableCell;
    }
    
    static NSString *cellID = @"recordCellID";
    UINib *cellNib = [UINib nibWithNibName:@"recordTabCell" bundle:nil];
    
    [tableView registerNib:cellNib forCellReuseIdentifier:cellID];
    
    
    recordTableViewCell  *tableCell= [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    tableCell.recordLab.text = [_informationArray objectAtIndex:indexPath.row];
    tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return tableCell;
}
//
-(void)GetMemberTime:(BOOL)isPackYears AndPayType:(BOOL)isAlpay{

    if (isPackYears == YES) {
        
        NSLog(@"包年");
    }else{
    
        NSLog(@"包月");
    }
    
    NSLog(@"%@",isAlpay?@"支付宝":@"微信" );
    
}

//seg 改变

-(void)touchLabelWithIndex:(NSInteger)index{
    
    if (index == 0) {
         _isSelectLastSeg = NO;
        _informationArray = _browseArray;
    }else if(index == 1){
        _isSelectLastSeg = NO;
        
        _informationArray = _collectArray;
    }else{
        
        _isSelectLastSeg = YES;
    }
    [_informationTab reloadData];
    
}


//Action 设置
-(IBAction)Action_setting{
    
    NSLog(@"设置");
}

//修改CGRectMake
CG_INLINE CGRect
CGRectMake2(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
    CGRect rect;
    
    
    
    float w = ScreenWidth/320.0;
    //    float w = ScreenHeight/480.0;
    
    //375 667
    //320 480
    rect = CGRectMake(x * w , y * w, width * w, height *w );
    
    return rect;
}

@end
